package com.coursehub.progress.controller;

import com.coursehub.progress.service.StatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 统计分析控制器
 * 提供与单体系统StatisticsController一致的API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/statistics")
@RequiredArgsConstructor
@Validated
public class StatisticsController {
    
    private final StatisticsService statisticsService;
    
    /**
     * 根据学生ID和课程ID获取课程的所有章节ID
     */
    @GetMapping("/chapters/student/{studentId}/course/{courseId}")
    public ResponseEntity<List<String>> getChapterIdsByStudentAndCourse(
            @PathVariable @NotBlank String studentId,
            @PathVariable @NotBlank String courseId) {
        
        log.info("获取学生课程章节ID: studentId={}, courseId={}", studentId, courseId);
        
        List<String> chapterIds = statisticsService.getChapterIdsByStudentAndCourse(studentId, courseId);
        return ResponseEntity.ok(chapterIds);
    }
    
    /**
     * 根据章节ID获取章节下的所有资料ID
     */
    @GetMapping("/materials/chapter/{chapterId}")
    public ResponseEntity<List<String>> getMaterialIdsByChapter(
            @PathVariable @NotBlank String chapterId) {
        
        log.info("获取章节资料ID: chapterId={}", chapterId);
        
        List<String> materialIds = statisticsService.getMaterialIdsByChapter(chapterId);
        return ResponseEntity.ok(materialIds);
    }
    
    /**
     * 根据章节ID获取章节实体类
     */
    @GetMapping("/chapters/{chapterId}")
    public ResponseEntity<Map<String, Object>> getChapterById(
            @PathVariable @NotBlank String chapterId) {
        
        log.info("获取章节信息: chapterId={}", chapterId);
        
        Map<String, Object> chapter = statisticsService.getChapterById(chapterId);
        return ResponseEntity.ok(chapter);
    }
    
    /**
     * 根据课程ID获取课程实体类
     */
    @GetMapping("/courses/{courseId}")
    public ResponseEntity<Map<String, Object>> getCourseById(
            @PathVariable @NotBlank String courseId) {
        
        log.info("获取课程信息: courseId={}", courseId);
        
        Map<String, Object> course = statisticsService.getCourseById(courseId);
        return ResponseEntity.ok(course);
    }
    
    /**
     * 根据资料ID获取资料实体类
     */
    @GetMapping("/materials/{materialId}")
    public ResponseEntity<Map<String, Object>> getMaterialById(
            @PathVariable @NotBlank String materialId) {
        
        log.info("获取资料信息: materialId={}", materialId);
        
        Map<String, Object> material = statisticsService.getMaterialById(materialId);
        return ResponseEntity.ok(material);
    }
    
    /**
     * 根据课程ID获取课程的所有章节ID
     */
    @GetMapping("/chapters/course/{courseId}")
    public ResponseEntity<List<String>> getChapterIdsByCourse(
            @PathVariable @NotBlank String courseId) {
        
        log.info("获取课程章节ID: courseId={}", courseId);
        
        List<String> chapterIds = statisticsService.getChapterIdsByCourse(courseId);
        return ResponseEntity.ok(chapterIds);
    }
    
    /**
     * 根据课程ID和学生ID获取学生的课程学习进度
     */
    @GetMapping("/progress/course/{courseId}/student/{studentId}")
    public ResponseEntity<Integer> getCourseProgress(
            @PathVariable @NotBlank String courseId,
            @PathVariable @NotBlank String studentId) {
        
        log.info("获取课程学习进度: courseId={}, studentId={}", courseId, studentId);
        
        Integer progress = statisticsService.getCourseProgress(courseId, studentId);
        return ResponseEntity.ok(progress);
    }
    
    /**
     * 根据章节ID和学生ID获取学生的章节学习进度
     */
    @GetMapping("/progress/chapter/{chapterId}/student/{studentId}")
    public ResponseEntity<Integer> getChapterProgress(
            @PathVariable @NotBlank String chapterId,
            @PathVariable @NotBlank String studentId) {
        
        log.info("获取章节学习进度: chapterId={}, studentId={}", chapterId, studentId);
        
        Integer progress = statisticsService.getChapterProgress(chapterId, studentId);
        return ResponseEntity.ok(progress);
    }
    
    /**
     * 根据资料ID和学生ID获取学生的资料学习进度
     */
    @GetMapping("/progress/material/{materialId}/student/{studentId}")
    public ResponseEntity<Integer> getMaterialProgress(
            @PathVariable @NotBlank String materialId,
            @PathVariable @NotBlank String studentId) {
        
        log.info("获取资料学习进度: materialId={}, studentId={}", materialId, studentId);
        
        Integer progress = statisticsService.getMaterialProgress(materialId, studentId);
        return ResponseEntity.ok(progress);
    }
    
    /**
     * 统计指定班级的课程完成率
     */
    @GetMapping("/completion-rate/class/{classId}")
    public ResponseEntity<Integer> getClassCompletionRate(
            @PathVariable @NotBlank String classId) {
        
        log.info("获取班级完成率: classId={}", classId);
        
        Integer completionRate = statisticsService.getClassCompletionRate(classId);
        return ResponseEntity.ok(completionRate);
    }
    
    /**
     * 统计指定练习的提交完成率
     */
    @GetMapping("/completion-rate/exercise/{exerciseId}")
    public ResponseEntity<Integer> getExerciseCompletionRate(
            @PathVariable @NotBlank String exerciseId) {
        
        log.info("获取练习完成率: exerciseId={}", exerciseId);
        
        Integer completionRate = statisticsService.getExerciseCompletionRate(exerciseId);
        return ResponseEntity.ok(completionRate);
    }
}